package com.topisv.tms.web.shipped;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.topisv.tms.entity.ShippedBatch;
import com.topisv.tms.entity.ShippedBox;
import com.topisv.tms.entity.ShippedBoxSkus;
import com.topisv.tms.service.shipped.ShippedBatchService;
import com.topisv.tms.service.shipped.ShippedBoxService;
import com.topisv.tms.service.shipped.ShippedBoxSkusService;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * 出库批次箱子管理
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping(value = "/shippedBox")
public class ShippedBoxController {
	private static final Logger log = LoggerFactory.getLogger(ShippedBoxController.class);
	
	@Autowired
	private ShippedBoxService shippedBoxService;
	@Autowired
	private ShippedBoxSkusService shippedBoxSkusService;
	@Autowired
	private ShippedBatchService shippedBatchService;

	@RequestMapping(value = "showBoxs/{batchCode}")
	public String showBoxs(@PathVariable("batchCode") String batchCode,
			Model model) {
		List<ShippedBox> shippedBoxs = null;
		ShippedBatch shippedBatch = null;
		Map<String, List<ShippedBoxSkus>> maps = new TreeMap<String, List<ShippedBoxSkus>>();
		try {
			shippedBatch = shippedBatchService.findByBatchCode(batchCode);
			shippedBoxs = shippedBoxService.findByBatchCode(batchCode);
			for (ShippedBox o : shippedBoxs) {
				maps.put(o.getBoxCode(),
						shippedBoxSkusService.findByBoxCode(o.getBoxCode()));
			}
			model.addAttribute("maps", maps);
			model.addAttribute("obj", shippedBatch);
		} catch (Exception e) {
			log.error("error",e);
		}
		return "shippedBox/showBoxs";
	}

	@RequestMapping(value = "exportExcel/{batchCode}")
	public void exportExcel(@PathVariable("batchCode") String batchCode,
			HttpServletResponse response) {
		List<ShippedBox> shippedBoxs = null;
		WritableWorkbook book = null;
		try {

			try {
				response.setContentType("application/vnd.ms-excel");
				response.setHeader("Content-disposition", "attachment; filename="
						+ batchCode + ".xls");
				shippedBoxs = shippedBoxService.findByBatchCode(batchCode);
				book = Workbook.createWorkbook(response.getOutputStream());
			} catch (Exception e) {
				
			}

			for (ShippedBox shippedBox : shippedBoxs) {
				WritableSheet sheet = null;
				List<ShippedBoxSkus> shippedBoxSkus = null;
				int length = 0;
				try {
					String boxCode = shippedBox.getBoxCode();

					sheet = book.createSheet(boxCode, 0);
					sheet.setColumnView(0, 22);
					sheet.setColumnView(1, 18);
					sheet.setColumnView(2, 15);

					Label label1 = new Label(0, 0, "商品编码");
					sheet.addCell(label1);

					Label label2 = new Label(1, 0, "数量");
					sheet.addCell(label2);
					Label label3 = new Label(2, 0, "备注");
					sheet.addCell(label3);

					shippedBoxSkus = shippedBoxSkusService.findByBoxCode(boxCode);
					length = shippedBoxSkus.size();
				} catch (Exception e) {
					
				}
				for (int i = 0; i < length; i++) {
					try {
						ShippedBoxSkus shippedBoxSkuList = shippedBoxSkus.get(i);
						String skuId = shippedBoxSkuList.getSkuId();
						Label labe21 = new Label(0, i + 1, skuId+"");
						sheet.addCell(labe21);

						Label labe22 = new Label(1, i + 1,
								shippedBoxSkuList.getRealQty() + "");
						sheet.addCell(labe22);

						Label labe23 = new Label(2, i + 1, "");
						sheet.addCell(labe23);
					} catch (Exception e) {
						
					}

				}

			
			}
			book.write();
			book.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			
		}

	}
}
